package com.damai.mvc.view.imple;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.ResourceLoader;
import org.beetl.core.Template;
import org.beetl.core.exception.BeetlException;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.core.resource.WebAppResourceLoader;

import com.citywithincity.utils.ConfigReader;
import com.citywithincity.utils.PathUtil;
import com.damai.action.IAction;
import com.damai.action.IRender;
import com.damai.action.utils.WebUtils;
import com.damai.mvc.view.View;

public class DefaultWebRender implements IRender<Object> {
	public static GroupTemplate template;
	public static final DefaultWebRender DEFAULT = new DefaultWebRender();
	static{
		try {
			String webloader = ConfigReader.getDefault().get("webloader");
			ResourceLoader resourceLoader;
			if("res".equals(webloader)){
				File path = PathUtil.getWebInfPath("");
				resourceLoader = new WebAppResourceLoader(path.getAbsolutePath());
			}else{
				resourceLoader = new ClasspathResourceLoader(DefaultWebRender.class.getClassLoader());
			}
			Configuration cfg = Configuration.defaultConfiguration();
			template = new GroupTemplate(resourceLoader, cfg);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public static Template getTemplate(String key){
		return template.getTemplate(key);
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public void render(IAction action,Object data, 
			HttpServletRequest request, 
			HttpServletResponse response)
			throws IOException {
		//data是什么
		if(data instanceof String){
			String result = (String)data;
			if(result.startsWith(WebUtils.REDIRECT)){
				String url = result.substring(WebUtils.REDIRECT.length());
				response.sendRedirect(url);
				return;
			}
			throw new RuntimeException("Cannot parse command " + data);
		}else if(data instanceof byte[]){
			byte[] bytes = (byte[])data;
			//下载文件
			response.getWriter().print(Base64.encodeBase64String(bytes));
			return;
			
		}else if(data instanceof View){
			((View)data).renderTo(response);
			return;
			
		}
		response.setContentType("text/html;charset=UTF-8");
		String key = action.getKey() + ".html";
		Template t = template.getTemplate(key);
		Map<String, Object> binding;
		if(data instanceof Map){
			binding = (Map<String, Object>) data;
		}else{
			binding= new HashMap<String, Object>();
		}
		Enumeration<String> enumeration = request.getAttributeNames();
		while(enumeration.hasMoreElements()){
			String name = enumeration.nextElement();
			Object value = request.getAttribute(name);
			binding.put(name, value);
		}
		t.binding(binding);
		try {
			t.renderTo(response.getOutputStream());
		} catch (BeetlException e) {
			throw new RuntimeException(e);
		}
	}

}
